package com.hsd.app.action;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import net.sf.json.JSONArray;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hsd.app.model.HsdDssRelation;
import com.hsd.app.model.HsdEffctRelation;
import com.hsd.app.model.HsdHpInfo;
import com.hsd.app.service.IDataManageService;
import com.hsd.commons.model.HsdAccount;
import com.hsd.core.page.entity.PageParams;
import com.hsd.core.page.entity.Pagination;
import com.hsd.core.utils.TimeUtil;


@Controller
@RequestMapping("/datamanage")
public class HpManageAction extends PLBaseAction{
	@Autowired
	private IDataManageService hpdataService;
	public void setHpdataService(IDataManageService hpdataService) {
		this.hpdataService = hpdataService;
	}
	/**
	 * 数据管理 ：加载调养方案信息列表
	 * @param pageParams
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/loadHpList.do")
	public String loadHpList(PageParams pageParams,HttpServletRequest request,ModelMap model){
		String hpname = request.getParameter("hpname"); 
		String hptype = request.getParameter("hptype"); 
		Pagination page = hpdataService.loadHpList(pageParams,hpname,hptype);
		
		model.addAttribute("page",page);
		model.addAttribute("hpname",hpname); 
		model.addAttribute("hptype",hptype); 
		return "app/data/data-hp-list";
	}
	
	 
	/**
	 * 数据管理 ：增加调养方案信息
	 * @param pageParams
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/addHpInfo.do")
	public String addHpInfo(HttpServletRequest request,ModelMap model){
		String id = request.getParameter("id");
		HsdHpInfo hpinfo = null;
		if(StringUtils.isNotEmpty(id)){
			hpinfo = (HsdHpInfo)this.hpdataService.findByID(HsdHpInfo.class, new Long(id)); 
		}
		model.put("hpinfo", hpinfo);
		return "app/data/insert-one-hp";
	}
	
	/**
	 * 新增一个调养方案
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping(value="/addOneHp.do",produces = {"application/json;charset=UTF-8"})
	public @ResponseBody String addOneHp(HsdHpInfo hpinfo,HttpServletRequest request,ModelMap model){
		Map query = new HashMap();
		query.put("hpName", hpinfo.getHpName());
		query.put("youxiaobj", new Long(1));
		Object existData = hpdataService.getUniqueObject(HsdHpInfo.class, query);
		if(null != existData){
			return "-1";
		}
//		//fbobj.setIsuse(1l);
		HsdAccount accInfo = this.getCurrentuser(request);

 		String hpName = request.getParameter("hpname");
 		String id = request.getParameter("id");
		String hptype = request.getParameter("hptype");
		String position = request.getParameter("position");
		String hpcontent = request.getParameter("hpcontent");
		String alertinfo = request.getParameter("alertinfo");
		//Long id = this.checkAndSaveSynames(request, null, hpdataService, hpName, 1, 5);
		if(StringUtils.isNotEmpty(id)){
			hpinfo = (HsdHpInfo)this.hpdataService.findByID(HsdHpInfo.class, new Long(id));
		}else{
			hpinfo.setId(this.hpdataService.getSeqNextVal("seq_hsd_dssinfo_id"));  
		}
		hpinfo.setHpName(hpName);
		hpinfo.setType(new Long(hptype));
//		hpinfo.setYingyongff(hpcontent);
		hpinfo.setHpcontent(hpcontent);
		hpinfo.setZhizuofangfa(alertinfo);
		hpinfo.setPosition(position);
		hpinfo.setYouxiaobj(PLCONSTANTS.YOUXIAOBJ_YX);
		hpinfo.setOpertime(TimeUtil.getCurrentDate());
		if(null != accInfo){
			hpinfo.setUserid(accInfo.getId());
			hpinfo.setUsername(accInfo.getUsername());
		}
		hpdataService.saveObject(hpinfo); 
		 
		return hpinfo.getId().toString();
	}
	/**
	 * 修改一个调养方案
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping(value="/editOneHp.do",produces = {"application/json;charset=UTF-8"})
	public String editOneHp(HsdHpInfo hpobj,HttpServletRequest request,ModelMap model){ 
		String rid = request.getParameter("id"); 
		HsdHpInfo hpinfo = (HsdHpInfo)this.hpdataService.findByID(HsdHpInfo.class, new Long(rid));
		List effectlist = hpdataService.findList(" select a,b from HsdEffctRelation a,HsdEffectinfo b where a.effectid=b.id and a.rid="+rid);
		List dsslist1 = hpdataService.findList(" select a,b from HsdDssRelation a,HsdDssinfo b where a.dssid=b.id and a.type=1 and a.rid="+rid);
		List dsslist2 = hpdataService.findList(" select a,b from HsdDssRelation a,HsdDssinfo b where a.dssid=b.id and a.type=2 and a.rid="+rid);
		model.put("hpinfo", hpinfo);
		model.put("effectlist", effectlist);
		model.put("dsslist1", dsslist1);
		model.put("dsslist2", dsslist2);
		return "app/data/update-one-hp";
	}
	/**
	 * 修改调养方案
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping(value="/selectOneHp.do")
	public @ResponseBody String selectOneHp(HsdHpInfo hpobj,HttpServletRequest request,ModelMap model){ 
		//hpdataService.save(userobj);
		return "修改成功！";
	}
	
	
	/**
	 * 删除调养方案
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping(value="/delOneHp.do",produces = {"application/json;charset=UTF-8"})
	public @ResponseBody String delOneHp(HttpServletRequest request,ModelMap model){
		String id = request.getParameter("id"); 
		HsdHpInfo fbObj = (HsdHpInfo)hpdataService.findByID(HsdHpInfo.class, new Long(id));
		//orgObj.setIsuse(0l);
		fbObj.setYouxiaobj(PLCONSTANTS.YOUXIAOBJ_WX);
		hpdataService.saveObject(fbObj);
		return "删除成功！";
	}
	
	/****************************************功效开始**************************************************************/
	/**
	 * 过滤选择功效
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping(value="/loadFilterEffects.do",produces = {"application/json;charset=UTF-8"})
	public @ResponseBody String loadFilterEffects(PageParams pageParams,HttpServletRequest request,ModelMap model){
		String effectname = request.getParameter("effectname");
		String hpid = request.getParameter("hpid");
		String pageJson = hpdataService.loadFilterEffects(pageParams,hpid,effectname);
		return pageJson;
	}
	
	/**
	 * 保存功效关系
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping(value="/saveEffectRelation.do",produces = {"application/json;charset=UTF-8"})
	public @ResponseBody String saveEffectRelation(HsdEffctRelation relation,HttpServletRequest request,ModelMap model){
		hpdataService.save(relation);
		return "保存成功！";
	}
	
	/**
	 * 删除功效关系
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping(value="/deleteEffectRelation.do",produces = {"application/json;charset=UTF-8"})
	public @ResponseBody String deleteEffectRelation(HsdEffctRelation relation,HttpServletRequest request,ModelMap model){
		hpdataService.deleteEffectRelation(relation);
		return "删除成功！";
	}
	
	/**
	 * 查询方案相关功效
	 * @param request
	 * @param model
	 * @return
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping(value="/loadHpEffecs1.do",produces = {"application/json;charset=UTF-8"})
	public @ResponseBody String loadHpEffecs1(HttpServletRequest request,ModelMap model){
		String rid = request.getParameter("rid"); 
		Map query = new HashMap();
		query.put("rid", Long.parseLong(rid));
		query.put("type", PLCONSTANTS.RELATION_HP);
		//List list = hpdataService.findList(HsdEffctRelation.class, query);
		List list = hpdataService.findList(" select a,b from HsdEffctRelation a,HsdEffectinfo b where a.effectid=b.id and a.rid="+rid);
		JSONArray result = new JSONArray();
		if(null != list){
			result = JSONArray.fromObject(list);
			System.out.println("===>>> " + result);
		}
		return result.toString();
	}
	/**
	 * 查询方案功效
	 * @param request
	 * @param model
	 * @return
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping(value="/loadHpEffecs.do",produces = {"application/json;charset=UTF-8"})
	public @ResponseBody String loadHpEffecs(HttpServletRequest request,ModelMap model){
		String rid = request.getParameter("rid"); 
		Map query = new HashMap();
		query.put("rid", Long.parseLong(rid));
		query.put("type", PLCONSTANTS.RELATION_HP);
		List list = hpdataService.findList(HsdEffctRelation.class, query);
		JSONArray result = new JSONArray();
		if(null != list){
			result = JSONArray.fromObject(list);
			System.out.println("===>>> " + result);
		}
		return result.toString();
	}
	
	/****************************************病症开始**************************************************************/
	/**
	 * 过滤选择功效
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping(value="/loadFilterDss.do",produces = {"application/json;charset=UTF-8"})
	public @ResponseBody String loadFilterDss(PageParams pageParams,HttpServletRequest request,ModelMap model){
		String dssname = request.getParameter("dssname"); 
		String hpid = request.getParameter("hpid");
		String pageJson = hpdataService.loadFilterDss(pageParams,hpid,dssname);
		return pageJson;
	}
	
	/**
	 * 保存病症关系
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping(value="/saveDssRelation.do",produces = {"application/json;charset=UTF-8"})
	public @ResponseBody String saveDssRelation(HsdDssRelation relation,HttpServletRequest request,ModelMap model){
		hpdataService.save(relation);
		return "保存成功！";
	}
	
	/**
	 * 更新病症关系
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping(value="/updateDssRelation.do",produces = {"application/json;charset=UTF-8"})
	public @ResponseBody String updateDssRelation(HsdDssRelation relation,HttpServletRequest request,ModelMap model){
		Map query = new HashMap();
		query.put("rid", relation.getRid());
		query.put("dssid", relation.getDssid());
		query.put("rtype", relation.getRtype());
		HsdDssRelation oldrelation = (HsdDssRelation)hpdataService.getUniqueObject(HsdDssRelation.class, query);
		oldrelation.setType(relation.getType());
		hpdataService.save(oldrelation);
		return "保存成功！";
	}
	
	/**
	 * 删除病症关系
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping(value="/deleteDssRelation.do",produces = {"application/json;charset=UTF-8"})
	public @ResponseBody String deleteDssRelation(HsdDssRelation relation,HttpServletRequest request,ModelMap model){
		hpdataService.deleteDssRelation(relation);
		return "删除成功！";
	}
	
	/**
	 * 查询方案相关的病症信息
	 * @param request
	 * @param model
	 * @return
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping(value="/loadHpDss1.do",produces = {"application/json;charset=UTF-8"})
	public @ResponseBody String loadHpDss1(HttpServletRequest request,ModelMap model){
		String rid = request.getParameter("rid"); 
		Map query = new HashMap();
		query.put("rid", Long.parseLong(rid));
		 
		List list = hpdataService.findList(" select a,b from HsdDssRelation a,HsdDssinfo b where a.dssid=b.id and a.rid="+rid);
		JSONArray result = new JSONArray();
		if(null != list){
			result = JSONArray.fromObject(list);
			System.out.println("===>>> " + result);
		}
		return result.toString();
	}
	
	/**
	 * 查询方案功效
	 * @param request
	 * @param model
	 * @return
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping(value="/loadHpDss.do",produces = {"application/json;charset=UTF-8"})
	public @ResponseBody String loadDss(HttpServletRequest request,ModelMap model){
		String rid = request.getParameter("rid"); 
		Map query = new HashMap();
		query.put("rid", Long.parseLong(rid));
		query.put("rtype", PLCONSTANTS.RELATION_HP);
		List list = hpdataService.findList(HsdDssRelation.class, query);
		JSONArray result = new JSONArray();
		if(null != list){
			result = JSONArray.fromObject(list);
			System.out.println("===>>> " + result);
		}
		return result.toString();
	}
	/**
	 * 查看调养方案信息
	 * @param request
	 * @param model
	 * @return
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping(value="/viewOneHp.do")
	public  String viewOneHp(HttpServletRequest request,ModelMap model){
		String rid = request.getParameter("id"); 
		HsdHpInfo hpinfo = (HsdHpInfo)this.hpdataService.findByID(HsdHpInfo.class, new Long(rid));
		model.put("hpinfo", hpinfo);
		return "app/data/view-one-hp";
	}
}
